// 函数防抖动
export function debounce(func, delay) {
  let timer;
  return function(...args) {
    if (timer) {
      clearTimeout(timer);
    }
    timer = setTimeout(() => {
      func.apply(this, args);
    }, delay);
  };
}
// 数字格式化
export function format_num(num) {
  if (!num) return num;
  if (num > 99999999) num = (num / 100000000).toFixed(1) + "亿";
  else if (num > 9999) num = (num / 10000).toFixed(1) + "w";
  else if (num < -99999999) num = (num / 100000000).toFixed(1) + "亿";
  else if (num < -9999) num = (num / 10000).toFixed(1) + "w";
  else if (num % 1 != 0) num = (num / 1).toFixed(1);
  else if (num % 1 == 0) num = num / 1;
  return num;
}
// 时间格式调整
export function initTime(timeStr) {
  if (timeStr == "--" || !timeStr) {
    return "--";
  } else {
    return timeStr.replace(/[h|m|s]/gi, a => {
      return `<span style="font-style:normal;font-size:18px;color:#222">${a}</span>`;
    });
  }
}
//毫秒格式化
export function format_time(num) {
  if (!num) return num;
  if(num < 1000) return '';
  let num1 = num / 1000;
  let d = parseInt(num1 / 86400);
  let h = parseInt((num1 % 86400) / 3600);
  let m = parseInt(((num1 % 86400) % 3600) / 60);
  let s = Math.round(((num1 % 86400) % 3600) % 60);
  if (d > 0) {
    return (num1 = d + "天" + h + "小时" + m + "分" + s + "秒");
  } else if (h > 0) {
    return (num1 = h + "小时" + m + "分" + s + "秒");
  } else if (m > 0) {
    return (num1 = m + "分" + s + "秒");
  } else {
    return (num1 = s + "秒");
  }
}
//毫秒格式化
export function format_timehm(num) {
  if (!num) return num;
  let num1 = num / 1000;
  let d = parseInt(num1 / 86400);
  let h = parseInt((num1 % 86400) / 3600);
  let m = parseInt(((num1 % 86400) % 3600) / 60);
  let s = Math.round(((num1 % 86400) % 3600) % 60);
  if (d > 0) {
    return (num1 = d + "天" + h + "小时" + m + "分");
  } else if (h > 0) {
    return (num1 = h + "小时" + m + "分");
  } else if (m > 0) {
    return (num1 = m + "分");
  }
}

// 把时间戳转为时间
export function timestamp(number, format) {
  if (number && number + "".length > 8) {
    var formateArr = ["Y", "M", "D", "h", "m", "s"];
    var returnArr = [];
    var date = new Date(number * 1000);
    returnArr.push(date.getFullYear());
    returnArr.push(formatNumberForTimestamp(date.getMonth() + 1));

    returnArr.push(formatNumberForTimestamp(date.getDate()));

    // returnArr.push(formatNumberForTimestamp(date.getHours()));
    returnArr.push(formatNumberForTimestamp(date.getHours()));
    returnArr.push(formatNumberForTimestamp(date.getMinutes()));
    returnArr.push(formatNumberForTimestamp(date.getSeconds()));

    for (var i in returnArr) {
      format = format.replace(formateArr[i], returnArr[i]);
    }
    return format;
  } else {
    return "";
  }
}
// 数字补0
export function formatNumberForTimestamp(n) {
  n = n.toString();
  return n[1] ? n : "0" + n;
}
// 把标准时间转换成特定格式日期
export function formatDate(date, fmt) {
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(
      RegExp.$1,
      (date.getFullYear() + "").substr(4 - RegExp.$1.length)
    );
  }
  let o = {
    "M+": date.getMonth() + 1,
    "d+": date.getDate(),
    "h+": date.getHours(),
    "m+": date.getMinutes(),
    "s+": date.getSeconds()
  };
  for (let k in o) {
    if (new RegExp(`(${k})`).test(fmt)) {
      let str = o[k] + "";
      fmt = fmt.replace(
        RegExp.$1,
        RegExp.$1.length === 1 ? str : padLeftZero(str)
      );
    }
  }
  return fmt;
}

function padLeftZero(str) {
  return ("00" + str).substr(str.length);
}
// 百分数格式化 num要格式化的数据 fixNum 保留几位小数 isAbs是否取绝对值
export function formatRate(num, fixNum, isAbs) {
  if (num == 0) {
    return 0;
  } else if (num == 1) {
    return 100 + "%";
  } else if (num == "") {
    return "";
  } else {
    // 取绝对值
    if (isAbs == 1) {
      return Math.abs((num * 100).toFixed(fixNum)) + "%";
    } else {
      return (num * 100).toFixed(fixNum) + "%";
    }
  }
}

// 数组去重复
export function unique(arr) {
  if (!Array.isArray(arr)) {
    return;
  }
  var array = [];
  for (var i = 0; i < arr.length; i++) {
    if (array.indexOf(arr[i]) === -1) {
      array.push(arr[i]);
    }
  }
  return array;
}

// 去除字符串两边的空白（空格）不用trim怕有的浏览器不兼容
export function trimStr(word) {
  return word.replace(/^\s+|\s+$/gm, "");
}

export function formateSeconds(endTime) {
  let secondTime = parseInt(endTime); //将传入的秒的值转化为Number
  let min = 0; // 初始化分
  let h = 0; // 初始化小时
  let result = "";
  if (secondTime > 60) {
    //如果秒数大于60，将秒数转换成整数
    min = parseInt(secondTime / 60); //获取分钟，除以60取整数，得到整数分钟
    secondTime = parseInt(secondTime % 60); //获取秒数，秒数取佘，得到整数秒数
    if (min > 60) {
      //如果分钟大于60，将分钟转换成小时
      h = parseInt(min / 60); //获取小时，获取分钟除以60，得到整数小时
      min = parseInt(min % 60); //获取小时后取佘的分，获取分钟除以60取佘的分
    }
  }
  result = `${h.toString().padStart(2, "0")}:${min
    .toString()
    .padStart(2, "0")}:${secondTime.toString().padStart(2, "0")}`;
  return result;
}

//获取随机字符串
export function getStr(len) {
  len = len || 32;
  let $chars =
    "ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678"; /****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/
  let maxPos = $chars.length;
  let pwd = "";
  for (let i = 0; i < len; i++) {
    pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
  }
  return pwd;
}

// 获取地址栏参数

function getUrlPms() {
  let search =
    window.location.search.substr(1) || window.location.hash.split("?")[1];
  if (search) {
    let params = search.split("&"),
      urlParams = {};
    for (let i = 0; i < params.length; i++) {
      let thisData = params[i].split("=");
      urlParams[thisData[0]] = decodeURIComponent(thisData[1]);
    }
    return urlParams;
  } else {
    return {};
  }
}
export let urlPms = getUrlPms();

// 日期格式化
export function formatDateNew(date, typeStr) {
  let thisDate = new Date();
  if (date) {
    thisDate = new Date(date);
  }
  let YY = thisDate.getFullYear(),
    Y = YY.toString().substr(2, 2),
    M = 1 + thisDate.getMonth(),
    MM = M > 9 ? M : "0" + M,
    D = thisDate.getDate(),
    DD = D > 9 ? D : "0" + D,
    h = thisDate.getHours(),
    hh = h > 9 ? h : "0" + h,
    m = thisDate.getMinutes(),
    mm = m > 9 ? m : "0" + m,
    s = thisDate.getSeconds(),
    ss = s > 9 ? s : "0" + s;
  let formatStr = typeStr
    .replace(/YY/g, YY)
    .replace(/Y/g, Y)
    .replace(/MM/g, MM)
    .replace(/M/g, M)
    .replace(/DD/g, DD)
    .replace(/D/g, D)
    .replace(/hh/g, hh)
    .replace(/h/g, h)
    .replace(/mm/g, mm)
    .replace(/m/g, m)
    .replace(/ss/g, ss)
    .replace(/s/g, s);
  return formatStr;
}
// 1574954488000

// 获取固定日期的星期
export function formatDateWeek(time) {
  //初始化日期时间
  let myDate = new Date(time);
  // var year=myDate.getFullYear();
  // var month=myDate.getMonth()+1;
  // var date=myDate.getDate();
  // console.log(year+"-"+month+"-"+date);
  let str = "星期" + "日一二三四五六".charAt(myDate.getDay());
  return str;
}